var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var login = require('./routes/login.js')
var register = require('./routes/register.js')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var search = require('./routes/search')
var createRouter = require('./routes/createTask')
var acceptRouter = require('./routes/rwxiangqing')//6.27 任务详情界面/任务接受界面
var jobfachu = require('./routes/jobfachu')//6.28我的发布任务列表
var jobjieshou = require('./routes/jobjieshou')//6.29我的接受任务列表
var processfachu = require('./routes/processfachu')//7.1
var processjieshou = require('./routes/processjieshou')//7.1
var updateNickname = require('./routes/updateNickname')

var verify = require('./routes/verify')
const cors = require('cors')
var lostpsw = require('./routes/lostpsw')
var uppicture = require('./routes/uppicture')


var app = express();

//work the cookie client sends
const session = require('express-session')
//session decoding
app.use(session({secret: 'session-key',resave:false,saveUninitialized:true})) 
app.use((req,res,next) =>{
  switch(req.url){
    case "/login":{
      console.log(req.url)
      req.session.userInfo = {
        _id: '6104fe44c1e10301d8efde54',
        username: 'www159',
        email: '2107795244@qq.com',
        password: '064919'
      }
    }
    default:
  }
  
  next()
})
app.use(cors())
app.use(
  express.static(path.join(__dirname,"/public"))
)
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/picture',(req,res,next) => {
  res.append('Access-Control-Allow-Origin', "*");
  console.log("最简单的中间件")
  next()
})
app.use(createRouter);
app.use(acceptRouter);//6.27 任务详情界面/任务接受界面
app.use(jobfachu);//6.28我的发布任务列表
app.use(jobjieshou);//6.29我的接受任务列表
app.use(processfachu)//7.1
app.use(processjieshou)//7.1
app.use(login)
app.use(updateNickname)
app.use(register)
app.use(search)
app.use(uppicture)
app.use(lostpsw)
app.use(verify)


                  //上面那行search放到这就不行，tmd为啥？

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});
app.listen(80, () =>{
  console.log("服务器启动...");
})
module.exports = app;